<template>
  <div class="news-view">
    <app-header></app-header>
    <div class="news-container">
      <div class="news-content">
        <div class="news-detail">
          <h1 class="news-title">{{ newsDetail.newsTitle }}</h1>
          <div class="news-meta">
            <span class="news-type">资讯类别：{{ newsDetail.newsType === 1 ? "通知" : '新闻' }}</span>
            <span class="news-date">发布时间：{{ newsDetail.publishTime }}</span>
          </div>
          <div class="news-content" v-html="newsDetail.newsContent"></div>
          <div class="news-attachment" v-if="newsDetail.newsAttachment && JSON.parse(newsDetail.newsAttachment).length > 0">
            <span class="attachment-label">附件:</span>
            <div class="attachment-list">
              <div v-for="(attachment, index) in JSON.parse(newsDetail.newsAttachment)" :key="index" class="attachment-item">
              <span @click="downloadFile(attachment)" class="attachment-link">
                {{ attachment.filename }}
              </span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <app-footer></app-footer>
  </div>
</template>

<script>
import AppHeader from '../components/AppHeader.vue'
import AppFooter from '../components/AppFooter.vue'
import {GetNewsDetail} from "@/service/news";

export default {
  name: 'NewsDetail',
  components: {
    AppHeader,
    AppFooter
  },
  data() {
    return {
      newsDetail: {
        newsTitle: '',
        newsType: '',
        publishTime: '',
        newsContent: '',
        newsAttachment: [],
        attachmentName: ''
      }
    }
  },
  mounted() {
    const newsId = this.$route.params.id;
    if (newsId) {
      this.fetchNewsDetail(newsId);
    } else {
      console.log('No news ID found in route params');
    }
  },
  methods: {
    fetchNewsDetail(newsId) {
      GetNewsDetail({
        id: newsId,
      }).then(response => {
        if (response && response.data) {
          this.newsDetail = response.data;
        }
      }).catch(error => {
        console.error('Error fetching news detail:', error);
      });
    },
    downloadFile(attachment) {
      const link = document.createElement('a');
      link.href = attachment.url.replace('http://124.16.154.205:9000/','/minioUrl/');
      link.download = attachment.filename;
      link.click();
    }
  },
  watch: {
    '$route'(to, from) {
      if (to.params.id && to.params.id !== from.params.id) {
        this.fetchNewsDetail(to.params.id);
      }
    }
  }
}
</script>

<style scoped>
.news-view {
  .news-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;

    .news-content {
      background: white;
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      padding: 20px;

      .news-detail {
        margin-bottom: 20px;

        .news-title {
          font-size: 24px;
          color: #0056b3;
          margin-bottom: 10px;
          text-align: center;
        }

        .news-meta {
          display: flex;
          gap: 100px;
          color: #666;
          margin-bottom: 20px;
          justify-content: center;
        }

        .news-content {
          font-size: 16px;
          color: #333;
          line-height: 1.6;
        }

        .news-attachment {
          margin-top: 20px;
          font-size: 14px;
          color: #333;

          .attachment-label {
            font-size: 16px; /* 调整"附件:"文字大小 */
            font-weight: bold;
            color: #333;
            margin-right: 8px;
          }

          .attachment-list {
            display: flex;
            flex-direction: column;
            gap: 4px;
            margin-top: 8px;

            .attachment-item {
              .attachment-link {
                color: #007bff;
                text-decoration: none;
                font-size: 14px;

                &:hover {
                  text-decoration: underline;
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>
